import {getLocalStore,setLocalStore,removeLocalStore} from './localStorage'
const CITY_PAGE_SEARCH ='city_search_addr' // 城市页面搜索 商圈/地名/小区/学校 历史
const SEARCH_PAGE = 'search_food' // 搜索页面，搜索历史
const USER= 'user'
const GEOHASH= 'geohash'
const SHOP_CART= 'shop_cart'
/**
 * 操作 地址搜索历史
 */
export const getAddrHistory=()=>{
    let res = getLocalStore(CITY_PAGE_SEARCH);
    if(res){
        res = JSON.parse(res);
    }else{
        res =[]
    }
    return res; 
}
export const setAddrHistory=(val)=>{
    let res = getAddrHistory(CITY_PAGE_SEARCH);
    res.push(val)
    setLocalStore(CITY_PAGE_SEARCH,JSON.stringify(res));
}
export const removeAddrHistory=()=>{
    removeLocalStore(CITY_PAGE_SEARCH);
}

/**
 * 操作 商家/美食 搜索历史
 */
export const getFoodHistory=()=>{
    let res = getLocalStore(SEARCH_PAGE);
    if(res){
        res = JSON.parse(res);
    }else{
        res =[]
    }
    return res; 
}
export const setFoodhHistory=(val,flag=false)=>{ // true 直接保存，flase 插入单个到res中
    if(flag){
        setLocalStore(SEARCH_PAGE,JSON.stringify(val));
    }else{
        let res = getFoodHistory(SEARCH_PAGE);
        res.push(val)
        setLocalStore(SEARCH_PAGE,JSON.stringify(res));
    }
    
}
export const removeFoodHistory=()=>{
    removeLocalStore(SEARCH_PAGE);
}


/**
 *  操作用户信息
 */

export const getUser=()=>{
    let res = getLocalStore(USER);
    if(res){
        res = JSON.parse(res);
    }else{
        res =null
    }
    return res;
}
export const setUser=(u)=>{
    setLocalStore(USER,JSON.stringify(u))
}
export const removeUser=()=>{
    removeLocalStore(USER);
}

// geohash
/*
export const getGeohash=()=>{
    let res = getLocalStore(GEOHASH);
    if(!res){
        res =null
    }
    return res;
}
export const setGeohash=(u)=>{
    setLocalStore(GEOHASH,u)
}
export const removeGeohash=()=>{
    removeLocalStore(GEOHASH);
}
*/
export const getShopCartHistory=()=>{
    let res = getLocalStore(SHOP_CART);
    if(!res){
        res =null
    }
    return JSON.parse(res);
}
export const setShopCartHistory=(val)=>{
    setLocalStore(SHOP_CART,JSON.stringify(val))
}

